package cn.iocoder.yudao.module.tms.controller.admin.customerfollowup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.customerfollowup.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.customerfollowup.CustomerFollowUpDO;
import cn.iocoder.yudao.module.tms.service.customerfollowup.CustomerFollowUpService;

@Tag(name = "管理后台 - 客户跟进")
@RestController
@RequestMapping("/tms/customer-follow-up")
@Validated
public class CustomerFollowUpController {

    @Resource
    private CustomerFollowUpService customerFollowUpService;

    @PostMapping("/create")
    @Operation(summary = "创建客户跟进")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:create')")
    public CommonResult<Long> createCustomerFollowUp(@Valid @RequestBody CustomerFollowUpSaveReqVO createReqVO) {
        return success(customerFollowUpService.createCustomerFollowUp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户跟进")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:update')")
    public CommonResult<Boolean> updateCustomerFollowUp(@Valid @RequestBody CustomerFollowUpSaveReqVO updateReqVO) {
        customerFollowUpService.updateCustomerFollowUp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户跟进")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:delete')")
    public CommonResult<Boolean> deleteCustomerFollowUp(@RequestParam("id") Long id) {
        customerFollowUpService.deleteCustomerFollowUp(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户跟进")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:delete')")
    public CommonResult<Boolean> deleteCustomerFollowUpList(@RequestParam("ids") List<Long> ids) {
        customerFollowUpService.deleteCustomerFollowUpListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户跟进")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:query')")
    public CommonResult<CustomerFollowUpRespVO> getCustomerFollowUp(@RequestParam("id") Long id) {
        CustomerFollowUpDO customerFollowUp = customerFollowUpService.getCustomerFollowUp(id);
        return success(BeanUtils.toBean(customerFollowUp, CustomerFollowUpRespVO.class));
    }

    @GetMapping("/list-by-company")
    @Operation(summary = "根据公司ID获取跟进记录列表")
    @Parameter(name = "companyId", description = "公司ID", required = true)
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:query')")
    public CommonResult<List<CustomerFollowUpRespVO>> getCustomerFollowUpListByCompanyId(@RequestParam("companyId") Long companyId) {
        List<CustomerFollowUpDO> list = customerFollowUpService.getCustomerFollowUpListByCompanyId(companyId);
        return success(BeanUtils.toBean(list, CustomerFollowUpRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户跟进分页")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:query')")
    public CommonResult<PageResult<CustomerFollowUpRespVO>> getCustomerFollowUpPage(@Valid CustomerFollowUpPageReqVO pageReqVO) {
        PageResult<CustomerFollowUpDO> pageResult = customerFollowUpService.getCustomerFollowUpPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerFollowUpRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户跟进 Excel")
    @PreAuthorize("@ss.hasPermission('tms:customer-follow-up:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerFollowUpExcel(@Valid CustomerFollowUpPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerFollowUpDO> list = customerFollowUpService.getCustomerFollowUpPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户跟进.xls", "数据", CustomerFollowUpRespVO.class,
                        BeanUtils.toBean(list, CustomerFollowUpRespVO.class));
    }

}
